package network.editor;

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import network.NetNode;

import org.apache.log4j.Logger;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;

/**
 * 图形的Vertex选择面板
 * 
 * @author zhangzuoqiang <br/>
 *         Date: Aug 25, 2011
 */
public class EditorImageVertexPanel extends AbstarctEditorPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger log = Logger
			.getLogger(EditorImageVertexPanel.class);

	public EditorImageVertexPanel() {
		init();
	}

	private void init() {
		CellResource imageResource = EditorResource.getImageResource();
		int imageNodeCount = imageResource.getNames().size();// 获得图片的数量

		if (imageNodeCount > 0) {
			setLayout(new GridLayout(0, 3));
			for (int i = 0; i < imageNodeCount; i++) {
				String cellStyle = String.format("image;image=/%s",
						imageResource.getIconPaths().get(i));
				ImageIcon sourceIcon = (ImageIcon) imageResource.getIcons()
						.get(i);// 原始图标,mxCell的默认width和height就是图标的原始大小
				String text = imageResource.getNames().get(i);
				NetNode netNode = new NetNode(text);
				// log.debug(netNode.getElement().getAttribute("text"));
				addImageVertex(text, sourceIcon, cellStyle,
						sourceIcon.getIconWidth(), sourceIcon.getIconHeight(),
						netNode.getElement());
			}
		}
	}

	/**
	 * 增加Image类型的vertex
	 * 
	 * @param name
	 *            图元名称
	 * @param icon
	 *            图元图标
	 * @param style
	 *            图元style
	 * @param width
	 *            图元的默认宽度
	 * @param height
	 *            图元的默认高度
	 * @param value
	 *            图元绑定的对象，默认绑定的是NetNode 徐骏 下午01:58:44
	 */
	public void addImageVertex(final String name, ImageIcon icon, String style,
			int width, int height, Object value) {

		mxCell cell = new mxCell(value, new mxGeometry(0, 0, width, height),
				style);
		cell.setVertex(true);
		log.debug(cell.getStyle());
		addImageLabel(name, icon, cell);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(3);
		frame.getContentPane().add(new EditorImageVertexPanel());
		frame.setVisible(true);

		frame.pack();
	}
}